﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Texfina.Core.Common;  
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;  
using Texfina.Entity.Rh;
using Texfina.Entity.Sy;
using Texfina.Entity.Mg;
using RHBOQry = Texfina.BOQry.Rh;
using SYBOQry = Texfina.BOQry.Sy;
using MGBOQry = Texfina.BOQry.Mg;

namespace RH
{
   public partial class wfPrograVacaBnd : SearchPage 
   {
      public const int _GRDLIST_IDESTADO = 8;
    
      protected void Page_Load(object sender, EventArgs e)
      {
                       
         this.LoadInitialSettings();
         if(this.IsPostBack){
            this.LoadInPostBack();
            return;
         } 

         this.LoadData();
         SearchData();

      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label) this.Master.FindControl("lblTitle");
         lbl.Text = "Programación de Vacaciones";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void LoadListControls()
      {
         EPlanilla objE = new EPlanilla();
         RHBOQry.IPlanilla objBO = (RHBOQry.IPlanilla)WCFHelper.GetObject<RHBOQry.IPlanilla>(typeof(RHBOQry.Planilla));
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         DataTable dt = objBO.GetListPlanillaVaciones(objE);
         WebHelper.LoadListControl(ddlidPlanilla, dt, "dsPlanilla", "idPlanilla");

         SYBOQry.IEmpresa objBOEmp = (SYBOQry.IEmpresa)WCFHelper.GetObject<SYBOQry.IEmpresa>(typeof(SYBOQry.Empresa));
         EEmpEstable objEEmp = new EEmpEstable();
         objEEmp.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         dt = objBOEmp.GetEstablecimientos(objEEmp);
         WebHelper.LoadListControl(ddlidEstable, dt, "dsEstable", "idEstable", ConstCustomFirstListItem.ITEM_ALL);

         IBOQuery objBOUNego = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(MGBOQry.UNegocio));
         EUNegocio objEUNeg = new EUNegocio();
         objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
         dt = objBOUNego.GetList(objEUNeg);
         WebHelper.LoadListControl(ddlidUniNego, dt, "dsUNegocio", "idUNegocio", ConstCustomFirstListItem.ITEM_ALL);

         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));
         ETablaGenDet objEDet = new ETablaGenDet();
         objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidFormaPago, dt, "dsDescrip", "idCodigo");

         objEDet.IdTabla = ConstMultitabla.MES;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidmes, dt, "dsDescrip", "idCodigo", ConstCustomFirstListItem.ITEM_ALL);
      
      }

      public override void SearchData()
      {

         EPersonalBandeja objE = new EPersonalBandeja();

         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         if (this.txtidPersonal.Text == "")
            objE.IdPersonal = null;
         else
            objE.IdPersonal = this.txtidPersonal.Text;

         if (this.txtnuDocIden.Text == "")
            objE.NuDocIden  = null;
         else
            objE.NuDocIden = this.txtnuDocIden.Text;

         if (this.txtdsPersonal.Text == "")
            objE.DsApellNom = null;
         else
            objE.DsApellNom = this.txtdsPersonal.Text;

         objE.IdPlanilla = ddlidPlanilla.SelectedItem.Value;

         if (this.ddlidEstable.Text == "")
             objE.IdEstable = null;
         else
             objE.IdEstable = this.ddlidEstable.Text;

         if (this.ddlidUniNego.Text == "")
             objE.IdUNegocio = null;
         else
             objE.IdUNegocio = this.ddlidUniNego.Text;

         if (this.ddlidCCosto.Text == "")
             objE.IdCCosto = null;
         else
             objE.IdCCosto = this.ddlidCCosto.Text;

         if (this.ddlidmes.Text == "")
             objE.IdMes = null;
         else
             objE.IdMes = this.ddlidmes.Text;

         objE.IdPeriodo = (int.Parse(Session[ConstSessionVar.PERIODO].ToString()) - 1).ToString();

         RHBOQry.IPersonal objBO = (RHBOQry.IPersonal)WCFHelper.GetObject<RHBOQry.IPersonal>(typeof(RHBOQry.Personal));
         DataTable dt = objBO.GetByCriteriaVacacion(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      protected void mnuToolbar_MenuItemClick(Object sender, MenuEventArgs e)
      {
         String strQS= "";

         switch(e.Item.Value){
            case ConstMenuToolbarValue.BUTTON_NEW:

               foreach (GridViewRow gvr in this.grdList.Rows)
               {
                  if (gvr.RowType == DataControlRowType.DataRow)
                  {
                     CheckBox chkAllSelect = (CheckBox)gvr.FindControl("chkSelected");
                     if (chkAllSelect.Checked)
                     {
                        strQS = strQS + gvr.Cells[2].Text + ",";
                     }
                  }
               }

               strQS = "wfPrograVacaMasiMnt.aspx?formEA=" + Convert.ToInt32(FormEditAction.AddNew).ToString() + "&IdPlanilla=" + ddlidPlanilla.SelectedItem.Value + "&IdPersonal=" + strQS + "&IdForPago=" + ddlidFormaPago.SelectedItem.Value;
               break; 
         }
         Response.Redirect(strQS);
      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      { 
         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         FormEditAction formEA= FormEditAction.AddNew;

         switch(e.CommandName)
         {
             case ConstGridRowCommand.GRIDEDIT:
               formEA = FormEditAction.Edit;
               break;
             case ConstGridRowCommand.GRIDDELETE:
               formEA = FormEditAction.Delete;
               break;
             case ConstGridRowCommand.GRIDPREVIEW:
               //this.PrintReport(Convert.ToInt32(e.CommandArgument));
               return;
         }
         String strQS = "wfPrograVacaMasiMnt.aspx?formEA=" + Convert.ToInt32(formEA).ToString() + "&IdPlanilla=" + ddlidPlanilla.SelectedItem.Value + "&IdPersonal=" + dr["idPersonal"].ToString() + "&IdForPago=" + ddlidFormaPago.SelectedItem.Value;
         Response.Redirect(strQS);
      }

      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.Header)
         {
            CheckBox chkAllChecked = new CheckBox();
            chkAllChecked.ID = "chkAllCheck";
            chkAllChecked.Checked = false;
            chkAllChecked.Attributes.Add("onClick", "SelectedCheckBoxAll(this);");
            e.Row.Cells[0].Controls.Add(chkAllChecked);
         }


         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             String strRI = e.Row.RowIndex.ToString();

                 if (e.Row.Cells[_GRDLIST_IDESTADO].Text ==  ConstFlagEstado.DESACTIVADO)
                 { e.Row.ForeColor = System.Drawing.Color.Green; }

         }

      }

      protected void bttOk_Click(object sender, EventArgs e)
      {
          this.SearchData();
      }

      protected void ddlidUNegocio_SelectedIndexChanged(object sender, EventArgs e)
      {
          ddlidCCosto.Items.Clear();
          EUNegocio objEUNeg = new EUNegocio();
          MGBOQry.IUNegocio objBOUNego = (MGBOQry.IUNegocio)WCFHelper.GetObject<MGBOQry.IUNegocio>(typeof(MGBOQry.UNegocio));
          objEUNeg.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();
          objEUNeg.IdUNegocio = ddlidUniNego.SelectedItem.Value;
          DataTable dt = objBOUNego.GetCCostoXUNegocio(objEUNeg);
          WebHelper.LoadListControl(ddlidCCosto, dt, "dsCCosto", "idCCosto", true, true);
      }
   }
}